<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveRecord::AttributeMethods::ClassMethods</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveRecord::AttributeMethods::ClassMethods</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/vendor/rails/activerecord/lib/active_record/attribute_methods_rb.html">
                vendor/rails/activerecord/lib/active_record/attribute_methods.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Declare and check for suffixed attribute methods.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M001094">attribute_method_suffix</a>&nbsp;&nbsp;
      <a href="#M001102">cache_attribute?</a>&nbsp;&nbsp;
      <a href="#M001100">cache_attributes</a>&nbsp;&nbsp;
      <a href="#M001101">cached_attributes</a>&nbsp;&nbsp;
      <a href="#M001097">define_attribute_methods</a>&nbsp;&nbsp;
      <a href="#M001099">define_read_methods</a>&nbsp;&nbsp;
      <a href="#M001096">generated_methods?</a>&nbsp;&nbsp;
      <a href="#M001098">instance_method_already_implemented?</a>&nbsp;&nbsp;
      <a href="#M001095">match_attribute_method?</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M001094" class="method-detail">
        <a name="M001094"></a>

        <div class="method-heading">
          <a href="#M001094" class="method-signature">
          <span class="method-name">attribute_method_suffix</span><span class="method-args">(*suffixes)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Declare a method available for all attributes with the given suffix. Uses
method_missing and respond_to? to rewrite the method
</p>
<pre>
  #{attr}#{suffix}(*args, &amp;block)
</pre>
<p>
to
</p>
<pre>
  attribute#{suffix}(#{attr}, *args, &amp;block)
</pre>
<p>
An attribute#{suffix} instance method must exist and accept at least the
attr argument.
</p>
<p>
For example:
</p>
<pre>
  class Person &lt; ActiveRecord::Base
    attribute_method_suffix '_changed?'

    private
      def attribute_changed?(attr)
        ...
      end
  end

  person = Person.find(1)
  person.name_changed?    # =&gt; false
  person.name = 'Hubert'
  person.name_changed?    # =&gt; true
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001094-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001094-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 38</span>
38:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">attribute_method_suffix</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">suffixes</span>)
39:         <span class="ruby-identifier">attribute_method_suffixes</span>.<span class="ruby-identifier">concat</span> <span class="ruby-identifier">suffixes</span>
40:         <span class="ruby-identifier">rebuild_attribute_method_regexp</span>
41:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001102" class="method-detail">
        <a name="M001102"></a>

        <div class="method-heading">
          <a href="#M001102" class="method-signature">
          <span class="method-name">cache_attribute?</span><span class="method-args">(attr_name)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001102-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001102-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 107</span>
107:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">cache_attribute?</span>(<span class="ruby-identifier">attr_name</span>)
108:         <span class="ruby-identifier">cached_attributes</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">attr_name</span>)
109:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001100" class="method-detail">
        <a name="M001100"></a>

        <div class="method-heading">
          <a href="#M001100" class="method-signature">
          <span class="method-name">cache_attributes</span><span class="method-args">(*attribute_names)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
<tt><a href="ClassMethods.html#M001100">cache_attributes</a></tt> allows
you to declare which converted attribute values should be cached. Usually
caching only pays off for attributes with expensive conversion methods,
like date columns (e.g. created_at, updated_at).
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001100-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001100-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 97</span>
97:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">cache_attributes</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">attribute_names</span>)
98:         <span class="ruby-identifier">attribute_names</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">attr</span><span class="ruby-operator">|</span> <span class="ruby-identifier">cached_attributes</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">attr</span>.<span class="ruby-identifier">to_s</span>}
99:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001101" class="method-detail">
        <a name="M001101"></a>

        <div class="method-heading">
          <a href="#M001101" class="method-signature">
          <span class="method-name">cached_attributes</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
returns the attributes where
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001101-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001101-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 102</span>
102:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">cached_attributes</span>
103:         <span class="ruby-ivar">@cached_attributes</span> <span class="ruby-operator">||=</span>
104:           <span class="ruby-identifier">columns</span>.<span class="ruby-identifier">select</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">c</span><span class="ruby-operator">|</span> <span class="ruby-identifier">attribute_types_cached_by_default</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">c</span>.<span class="ruby-identifier">type</span>)}.<span class="ruby-identifier">map</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">:name</span>).<span class="ruby-identifier">to_set</span>
105:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001097" class="method-detail">
        <a name="M001097"></a>

        <div class="method-heading">
          <a href="#M001097" class="method-signature">
          <span class="method-name">define_attribute_methods</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
generates all the attribute related methods for columns in the database
accessors, mutators and query methods
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001097-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001097-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 61</span>
61:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">define_attribute_methods</span>
62:         <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">generated_methods?</span>
63:         <span class="ruby-identifier">columns_hash</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">name</span>, <span class="ruby-identifier">column</span><span class="ruby-operator">|</span>
64:           <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">instance_method_already_implemented?</span>(<span class="ruby-identifier">name</span>)
65:             <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">serialized_attributes</span>[<span class="ruby-identifier">name</span>]
66:               <span class="ruby-identifier">define_read_method_for_serialized_attribute</span>(<span class="ruby-identifier">name</span>)
67:             <span class="ruby-keyword kw">else</span>
68:               <span class="ruby-identifier">define_read_method</span>(<span class="ruby-identifier">name</span>.<span class="ruby-identifier">to_sym</span>, <span class="ruby-identifier">name</span>, <span class="ruby-identifier">column</span>)
69:             <span class="ruby-keyword kw">end</span>
70:           <span class="ruby-keyword kw">end</span>
71: 
72:           <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">instance_method_already_implemented?</span>(<span class="ruby-node">&quot;#{name}=&quot;</span>)
73:             <span class="ruby-identifier">define_write_method</span>(<span class="ruby-identifier">name</span>.<span class="ruby-identifier">to_sym</span>)
74:           <span class="ruby-keyword kw">end</span>
75: 
76:           <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">instance_method_already_implemented?</span>(<span class="ruby-node">&quot;#{name}?&quot;</span>)
77:             <span class="ruby-identifier">define_question_method</span>(<span class="ruby-identifier">name</span>)
78:           <span class="ruby-keyword kw">end</span>
79:         <span class="ruby-keyword kw">end</span>
80:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001099" class="method-detail">
        <a name="M001099"></a>

        <div class="method-heading">
          <span class="method-name">define_read_methods</span><span class="method-args">()</span>
        </div>
      
        <div class="method-description">
          <p>
Alias for <a href="ClassMethods.html#M001097">define_attribute_methods</a>
</p>
        </div>
      </div>

      <div id="method-M001096" class="method-detail">
        <a name="M001096"></a>

        <div class="method-heading">
          <a href="#M001096" class="method-signature">
          <span class="method-name">generated_methods?</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001096-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001096-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 55</span>
55:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">generated_methods?</span>
56:         <span class="ruby-operator">!</span><span class="ruby-identifier">generated_methods</span>.<span class="ruby-identifier">empty?</span>
57:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001098" class="method-detail">
        <a name="M001098"></a>

        <div class="method-heading">
          <a href="#M001098" class="method-signature">
          <span class="method-name">instance_method_already_implemented?</span><span class="method-args">(method_name)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Check to see if the method is defined in the model or any of its subclasses
that also derive from ActiveRecord. Raise <a
href="../DangerousAttributeError.html">DangerousAttributeError</a> if the
method is defined by ActiveRecord though.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001098-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001098-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 84</span>
84:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">instance_method_already_implemented?</span>(<span class="ruby-identifier">method_name</span>)
85:         <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">true</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">method_name</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp re">/^id(=$|\?$|$)/</span>
86:         <span class="ruby-ivar">@_defined_class_methods</span>         <span class="ruby-operator">||=</span> <span class="ruby-constant">Set</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">ancestors</span>.<span class="ruby-identifier">first</span>(<span class="ruby-identifier">ancestors</span>.<span class="ruby-identifier">index</span>(<span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>)).<span class="ruby-identifier">collect!</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">m</span><span class="ruby-operator">|</span> <span class="ruby-identifier">m</span>.<span class="ruby-identifier">public_instance_methods</span>(<span class="ruby-keyword kw">false</span>) <span class="ruby-operator">|</span> <span class="ruby-identifier">m</span>.<span class="ruby-identifier">private_instance_methods</span>(<span class="ruby-keyword kw">false</span>) <span class="ruby-operator">|</span> <span class="ruby-identifier">m</span>.<span class="ruby-identifier">protected_instance_methods</span>(<span class="ruby-keyword kw">false</span>) }.<span class="ruby-identifier">flatten</span>)
87:         <span class="ruby-ivar">@@_defined_activerecord_methods</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">Set</span>.<span class="ruby-identifier">new</span>(<span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">public_instance_methods</span>(<span class="ruby-keyword kw">false</span>) <span class="ruby-operator">|</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">private_instance_methods</span>(<span class="ruby-keyword kw">false</span>) <span class="ruby-operator">|</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">protected_instance_methods</span>(<span class="ruby-keyword kw">false</span>))
88:         <span class="ruby-identifier">raise</span> <span class="ruby-constant">DangerousAttributeError</span>, <span class="ruby-node">&quot;#{method_name} is defined by ActiveRecord&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@@_defined_activerecord_methods</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">method_name</span>)
89:         <span class="ruby-ivar">@_defined_class_methods</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">method_name</span>)
90:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001095" class="method-detail">
        <a name="M001095"></a>

        <div class="method-heading">
          <a href="#M001095" class="method-signature">
          <span class="method-name">match_attribute_method?</span><span class="method-args">(method_name)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns MatchData if method_name is an attribute method.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001095-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001095-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 44</span>
44:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">match_attribute_method?</span>(<span class="ruby-identifier">method_name</span>)
45:         <span class="ruby-identifier">rebuild_attribute_method_regexp</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-keyword kw">defined?</span>(<span class="ruby-ivar">@@attribute_method_regexp</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-ivar">@@attribute_method_regexp</span>
46:         <span class="ruby-ivar">@@attribute_method_regexp</span>.<span class="ruby-identifier">match</span>(<span class="ruby-identifier">method_name</span>)
47:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>